//
//  RootViewController.m
//  中关村在线
//
//  Created by dllo on 16/1/9.
//  Copyright © 2016年 guoguo. All rights reserved.
//

#import "RootViewController.h"
#import "NetWorkHandler.h"
#import "HGTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "News.h"
#import "HGDB.h"
#import "HGCollectionViewCell.h"
#import "Picture.h"
#import "CaiDanViewController.h"
#import "TOPCollectionViewCell.h"

#import "SecondViewController.h"


#define UIColorFromRGB(rgbValue) [UIColor \
colorWithRed:((float)((rgbValue & 0xFF0000) >> 15))/255.0 \
green:((float)((rgbValue & 0xFF00) >> 8))/255.0 \
blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]

@interface RootViewController ()<  UIWebViewDelegate,UIScrollViewDelegate,UICollectionViewDataSource, UICollectionViewDelegate, TiaoZhuan>



@property (nonatomic,retain) NSMutableArray *arrCollection;
@property (nonatomic, retain) UIScrollView *scrollView;
@property (nonatomic, retain) UICollectionView *colloectionBig;
@property (nonatomic, retain) UIButton *button;
@property (nonatomic, retain) UIView *view1;
@property (nonatomic, retain) UICollectionView *TopCollectionView;
@property (nonatomic, assign) NSInteger a;


@end

@implementation RootViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)dealloc{
    [_scrollView release];
    [_arrCollection release];
    [_colloectionBig release];
   
    
    
    [super dealloc];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBar.barTintColor = UIColorFromRGB(0x175ccc);
    
    self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
    
    [self initCollectionViewBigLow];
    
    
    
    
    
    //UINavgetionController 数组
      self.arrCollection = [NSMutableArray arrayWithObjects:@"头条", @"订阅", @"苹果", @"新闻", @"手机", @"测评", @"数码", @"电脑", @"热捧", @"导购", @"视频", @"攒机", @"外设", @"摄影", @"直播", nil];
    

    [self initTopCollectionView];
    
    UIBarButtonItem *item= [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(itemAction:)];
    
    self.navigationItem.rightBarButtonItem = item;
    self.navigationItem.titleView = _TopCollectionView;
    
    
    [self initCaiDanView];
    
    
    
}

//完成跳转协议内容

- (void)TiaoZhuanWith:(News *)model{
    
    SecondViewController *sec = [[SecondViewController alloc] init];
    sec.model = model;
    [self.navigationController pushViewController:sec animated:YES];
    
}

- (void)initCaiDanView{
    
    self.view1 = [[UIView alloc] initWithFrame:CGRectMake(0, -667, self.view.frame.size.width, self.view.frame.size.height)];
    _view1.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:_view1];
    
    
    [_view1 release];
    
    
}

- (void)itemAction:(UIBarButtonItem *)item{
    

    
    
    
    
}




//创建顶部的collectionView
- (void)initTopCollectionView{
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    layout.itemSize = CGSizeMake(60, 30);
    
    
    
    self.TopCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 340, 30) collectionViewLayout:layout];
    
    _TopCollectionView.delegate = self;
    _TopCollectionView.dataSource = self;
    _TopCollectionView.backgroundColor = UIColorFromRGB(0x175ccc);
    _TopCollectionView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview: _TopCollectionView];
    
    [_TopCollectionView release];
    
    //注册
    
    [_TopCollectionView registerClass:[TOPCollectionViewCell class] forCellWithReuseIdentifier:@"cellTop"];
    
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return _arrCollection.count;

}






- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    

    //判断如果是上层的
    if (collectionView == _TopCollectionView) {
        
        TOPCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellTop" forIndexPath:indexPath];
        
        if (indexPath.row == 0) {
            cell.label.font = [UIFont systemFontOfSize:21];
            cell.label.text = _arrCollection[indexPath.row];
            cell.label.tag = 1000 + indexPath.row;
            cell.tag = 2000 + indexPath.row;
            [self collectionView:_TopCollectionView didSelectItemAtIndexPath:indexPath];
        }
        
        cell.label.text = _arrCollection[indexPath.row];
        cell.label.tag = 1000 + indexPath.row;
        
        return cell;
        
        
    }else{
        
        if (indexPath.row == 0) {
            
            HGCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell0" forIndexPath:indexPath];
         
            cell.block(@"http://lib.wap.zol.com.cn/ipj/docList/?v=6.0&class_id=0&page=1&vs=and412&retina=1&last_time=2016-01-10%2013:40");
            cell.delegate = self;
            cell.indexPath1 = indexPath;
            
            cell.tag = 1000;
            cell.backgroundColor = [UIColor whiteColor];
            //恢复smallScrollView的交互
            if (_colloectionBig.contentOffset.x > 0 ) {
                
                cell.smallScrollView.userInteractionEnabled = YES;
                
            }
            

                       return cell;
        } else if (indexPath.row == 1) {
            
            HGCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell1" forIndexPath:indexPath];
             cell.delegate = self;
            cell.block(@"http://lib.wap.zol.com.cn/ipj/docList/?v=6.0&class_id=255&isReviewing=NO&last_time=2016-01-13%2019%3A39&page=1&retina=1&vs=iph442");
            cell.indexPath1 = indexPath;
            //恢复smallScrollView的交互
            if (_colloectionBig.contentOffset.x > 0 ) {
                
                cell.smallScrollView.userInteractionEnabled = YES;
                
            }
            

            return cell;
            
        }else if (indexPath.row == 2){
            
            HGCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell2" forIndexPath:indexPath];
             cell.delegate = self;
            cell.block(@"http://lib.wap.zol.com.cn/ipj/docList/?v=6.0&class_id=255&isReviewing=NO&last_time=2016-01-13%2019%3A39&page=1&retina=1&vs=iph442");
            cell.indexPath1 = indexPath;
            //恢复smallScrollView的交互
            if (_colloectionBig.contentOffset.x > 0 ) {
                
                cell.smallScrollView.userInteractionEnabled = YES;
                
            }
            

            return cell;
        }else if (indexPath.row == 3){
            HGCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell3" forIndexPath:indexPath];
             cell.delegate = self;
            cell.block(@"http://lib.wap.zol.com.cn/ipj/docList/?v=6.0&class_id=1&isReviewing=NO&last_time=2016-01-15%2017%3A43&page=1&retina=1&vs=iph442");
            cell.indexPath1 = indexPath;
            //恢复smallScrollView的交互
            if (_colloectionBig.contentOffset.x > 0 ) {
                
                cell.smallScrollView.userInteractionEnabled = YES;
                
            }
            

            return cell;
        }else if (indexPath.row == 4){
            
            HGCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell4" forIndexPath:indexPath];
            cell.delegate = self;
            cell.block(@"http://lib.wap.zol.com.cn/ipj/docList/?v=6.0&class_id=74&isReviewing=NO&last_time=2016-01-17%2015%3A57&page=1&retina=1&vs=iph442");
            cell.indexPath1 = indexPath;
            //恢复smallScrollView的交互
            if (_colloectionBig.contentOffset.x > 0 ) {
                
                cell.smallScrollView.userInteractionEnabled = YES;
                
            }
            

            return cell;

        }else if (indexPath.row == 5){
            HGCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell5" forIndexPath:indexPath];
            cell.delegate = self;
            cell.block(@"http://lib.wap.zol.com.cn/ipj/docList/?v=6.0&class_id=2&isReviewing=NO&last_time=2016-01-17%2016%3A03&page=1&retina=1&vs=iph442");
            cell.indexPath1 = indexPath;
            //恢复smallScrollView的交互
            if (_colloectionBig.contentOffset.x > 0 ) {
                
                cell.smallScrollView.userInteractionEnabled = YES;
                
            }
            

            return cell;
        }else if (indexPath.row == 6){
            HGCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell6" forIndexPath:indexPath];
            cell.delegate = self;
            cell.block(@"http://lib.wap.zol.com.cn/ipj/docList/?v=6.0&class_id=7&isReviewing=NO&last_time=2016-01-17%2016%3A06&page=1&retina=1&vs=iph442");
            cell.indexPath1 = indexPath;
            //恢复smallScrollView的交互
            if (_colloectionBig.contentOffset.x > 0 ) {
                
                cell.smallScrollView.userInteractionEnabled = YES;
                
            }
            

            return cell;

        }else if (indexPath.row == 7){
            HGCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell7" forIndexPath:indexPath];
            cell.delegate = self;
            cell.block(@"http://lib.wap.zol.com.cn/ipj/docList/?v=6.0&class_id=210&isReviewing=NO&last_time=2016-01-17%2016%3A52&page=1&retina=1&vs=iph442");
            cell.indexPath1 = indexPath;
            //恢复smallScrollView的交互
            if (_colloectionBig.contentOffset.x > 0 ) {
                
                cell.smallScrollView.userInteractionEnabled = YES;
                
            }
            

            return cell;

        }else if (indexPath.row == 8){
            HGCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell8" forIndexPath:indexPath];
            cell.delegate = self;
            cell.block(@"http://lib.wap.zol.com.cn/ipj/docList/?v=6.0&class_id=8&isReviewing=NO&last_time=2016-01-15%2022%3A11&page=1&retina=1&vs=iph442");
            cell.indexPath1 = indexPath;
            
            return cell;
        }else if (indexPath.row == 9){
            HGCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell9" forIndexPath:indexPath];
            cell.delegate = self;
            cell.block(@"http://lib.wap.zol.com.cn/ipj/docList/?v=6.0&class_id=6&isReviewing=NO&last_time=2016-01-17%2017%3A18&page=1&retina=1&vs=iph442");
            cell.indexPath1 = indexPath;
            
            return cell;
        } else if (indexPath.row == 10){
            HGCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell10" forIndexPath:indexPath];
            cell.delegate = self;
            cell.block(@"http://lib.wap.zol.com.cn/ipj/docList/?v=6.0&class_id=9&isReviewing=NO&last_time=2016-01-17%2017%3A19&page=1&retina=1&vs=iph442");
            cell.indexPath1 = indexPath;
            
            return cell;
        } else if (indexPath.row == 11){
            HGCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell11" forIndexPath:indexPath];
            cell.delegate = self;
            cell.block(@"http://lib.wap.zol.com.cn/ipj/docList/?v=6.0&class_id=165&isReviewing=NO&last_time=2016-01-17%2018%3A45&page=1&retina=1&vs=iph442");
            cell.indexPath1 = indexPath;
            
            return cell;
        }else if (indexPath.row == 12){
            HGCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell12" forIndexPath:indexPath];
            cell.delegate = self;
            cell.block(@"http://lib.wap.zol.com.cn/ipj/docList/?v=6.0&class_id=353&isReviewing=NO&last_time=2016-01-17%2018%3A43&page=1&retina=1&vs=iph442");
            cell.indexPath1 = indexPath;
            
            return cell;
        } else if (indexPath.row == 13){
            HGCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell13" forIndexPath:indexPath];
            cell.delegate = self;
            cell.block(@"http://lib.wap.zol.com.cn/ipj/docList/?v=6.0&class_id=145&isReviewing=NO&last_time=2016-01-17%2018%3A52&page=1&retina=1&vs=iph442");
            cell.indexPath1 = indexPath;
            
            return cell;
        }else if (indexPath.row == 14){
            
            HGCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell3" forIndexPath:indexPath];
            cell.delegate = self;
            cell.block(@"http://lib.wap.zol.com.cn/ipj/docList/?v=6.0&class_id=15&isReviewing=NO&last_time=2016-01-17%2018%3A49&page=1&retina=1&vs=iph442");
            cell.indexPath1 = indexPath;
            
            return cell;
        }
    }
    
    
    return nil;
}




//collectionView点击方法放大字体

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
   
    if (collectionView == _TopCollectionView) {
        NSLog(@"%ld", indexPath.row);
        _colloectionBig.contentOffset = CGPointMake(self.view.frame.size.width * indexPath.row, 0);
        
        UILabel *label = [_TopCollectionView viewWithTag:1000 + indexPath.row];
        
        label.font = [UIFont systemFontOfSize:21];
        label.textColor = [UIColor whiteColor];
        
        
        
        UILabel *label1 = [_TopCollectionView viewWithTag:1000 + indexPath.row - 1];
        label1.font = [UIFont systemFontOfSize:16];
        UILabel *label2 = [_TopCollectionView viewWithTag:1000 + indexPath.row + 1];
        label2.font = [UIFont systemFontOfSize:16];
        
        
    }
    
    
    
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    self.a = _colloectionBig.contentOffset.x / self.view.frame.size.width;
    
    
    UILabel *label = [_TopCollectionView viewWithTag:1000 + _a];
    label.font = [UIFont systemFontOfSize:21];
    
    UILabel *label1 = [_TopCollectionView viewWithTag:1000 + _a - 1];
    label1.font = [UIFont systemFontOfSize:16];
    UILabel *label2 = [_TopCollectionView viewWithTag:1000 + _a + 1];
    label2.font = [UIFont systemFontOfSize:16];
    
    
}



- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{
   

        
        if (_a > 1) {
        
            _TopCollectionView.contentOffset = CGPointMake( (_a - 2) * 65, 0);
            
            
        
        }
        
        
    
    
    
    
}


- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
    if (collectionView == _TopCollectionView) {
        
        UILabel *label = [_TopCollectionView viewWithTag:1000 + indexPath.row];
        
        label.font = [UIFont systemFontOfSize:16];
        
    }
    
    
}






#pragma mark   最底层的COLLECTIONVIEW

- (void)initCollectionViewBigLow{
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    layout.itemSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height);
    
    
    self.colloectionBig = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) collectionViewLayout:layout];
   
    _colloectionBig.delegate = self;
    _colloectionBig.dataSource = self;
    _colloectionBig.pagingEnabled = YES;
    _colloectionBig.backgroundColor = [UIColor whiteColor];
    [self.view addSubview: _colloectionBig];;
    
    [_colloectionBig release];
    
    
    //注册
    
    [_colloectionBig registerClass:[HGCollectionViewCell class] forCellWithReuseIdentifier:@"cell0"];
    
    [_colloectionBig registerClass:[HGCollectionViewCell class] forCellWithReuseIdentifier:@"cell2"];
    
    [_colloectionBig registerClass:[HGCollectionViewCell class] forCellWithReuseIdentifier:@"cell3"];
    
    [_colloectionBig registerClass:[HGCollectionViewCell class] forCellWithReuseIdentifier:@"cell1"];
    [_colloectionBig registerClass:[HGCollectionViewCell class] forCellWithReuseIdentifier:@"cell4"];
    [_colloectionBig registerClass:[HGCollectionViewCell class] forCellWithReuseIdentifier:@"cell5"];
    [_colloectionBig registerClass:[HGCollectionViewCell class] forCellWithReuseIdentifier:@"cell6"];
   [_colloectionBig registerClass:[HGCollectionViewCell class] forCellWithReuseIdentifier:@"cell7"];
    [_colloectionBig registerClass:[HGCollectionViewCell class] forCellWithReuseIdentifier:@"cell8"];
    [_colloectionBig registerClass:[HGCollectionViewCell class] forCellWithReuseIdentifier:@"cell9"];
    [_colloectionBig registerClass:[HGCollectionViewCell class] forCellWithReuseIdentifier:@"cell10"];
    [_colloectionBig registerClass:[HGCollectionViewCell class] forCellWithReuseIdentifier:@"cell11"];
    [_colloectionBig registerClass:[HGCollectionViewCell class] forCellWithReuseIdentifier:@"cell12"];
    [_colloectionBig registerClass:[HGCollectionViewCell class] forCellWithReuseIdentifier:@"cell13"];
}






    
    
    


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
